Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 597)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.99568 12.99380 12.99196 12.99018 12.98843 12.98673 12.98506 12.98341
## [9] 12.98179 12.98018 12.97858 12.97699 12.97539 12.97379 12.97219 12.97056
## [17] 12.96891 12.96723 12.96553 12.96378 12.96199 12.96015 12.95826 12.95630
## [25] 12.95429 12.95220 12.95003 12.94779 12.94545 12.94304 12.94057 12.93804
## [33] 12.93546 12.93284 12.93017 12.92747 12.92473 12.92197 12.91919 12.91639
## [41] 12.91357 12.91075 12.90793 12.90511 12.90230 12.89950 12.89671 12.89395
## [49] 12.89122 12.88851 12.88585 12.88322 12.88065 12.87812 12.87565 12.87324
## [57] 12.87090 12.86863 12.86643 12.86431 12.86229 12.86035 12.85850 12.85676
## [65] 12.85499 12.85306 12.85100 12.84881 12.84650 12.84409 12.84159 12.83900
## [73] 12.83634 12.83363 12.83086 12.82807 12.82525 12.82242 12.81959 12.81677
## [81] 12.81398 12.81122 12.80850 12.80585 12.80327 12.80076 12.79836 12.79605
## [89] 12.79387 12.79181 12.78990 12.78813 12.78653 12.78511 12.78387 12.78283
## [97] 12.78200 12.78140 12.78102 12.78041 12.77913 12.77722 12.77475 12.77177
## [105] 12.76835 12.76455 12.76041 12.75600 12.75138 12.74661 12.74173 12.73682
## [113] 12.73193 12.72711 12.72243 12.71794 12.71370 12.70978 12.70622 12.70308
## [121] 12.70043 12.69832 12.69681 12.69596 12.69583 12.69647 12.69794 12.70030
## [129] 12.70408 12.70965 12.71682 12.72541 12.73524 12.74611 12.75786 12.77028
## [137] 12.78320 12.79644 12.80981 12.82313 12.83621 12.84887 12.86092 12.87219
## [145] 12.88248 12.89161 12.89941 12.90839 12.92085 12.93620 12.95388 12.97330
## [153] 12.99390 13.01510 13.03633 13.05702 13.07659 13.09447 13.11009 13.12287
## [161] 13.13224 13.14066 13.15084 13.16257 13.17563 13.18982 13.20493 13.22073
## [169] 13.23703 13.25361 13.27026 13.28677 13.30293 13.31853 13.33335 13.34719
## [177] 13.35983 13.37107 13.38069 13.38849 13.39424 13.39775 13.40031 13.40329
## [185] 13.40661 13.41014 13.41381 13.41750 13.42111 13.42455 13.42771 13.43050
## [193] 13.43281 13.43454 13.43560 13.43588 13.43528 13.43370 13.43104 13.42720
## [201] 13.42209 13.41559 13.40762 13.39806 13.38621 13.37166 13.35472 13.33572
## [209] 13.31498 13.29284 13.26961 13.24562 13.22120 13.19667 13.17236 13.14859
## [217] 13.12568 13.10397 13.08377 13.06156 13.03415 13.00236 12.96702 12.92894
## [225] 12.88896 12.84790 12.80659 12.76585 12.72650 12.68937 12.65528 12.62506
## [233] 12.59954 12.57534 12.54876 12.52013 12.48975 12.45794 12.42503 12.39132
## [241] 12.35715 12.32282 12.28865 12.25496 12.22207 12.19030 12.15996 12.13137
## [249] 12.10485 12.08071 12.05928 12.04087 12.02544 12.01249 12.00170 11.99273
## [257] 11.98525 11.97892 11.97340 11.96837 11.96349 11.95842 11.95284 11.94640
## [265] 11.93877 11.92962 11.92137 11.91637 11.91419 11.91439 11.91652 11.92016
## [273] 11.92486 11.93019 11.93571 11.94099 11.94557 11.94903 11.95094 11.95084
## [281] 11.94831 11.94290 11.93660 11.93155 11.92753 11.92432 11.92172 11.91949
## [289] 11.91744 11.91534 11.91298 11.91014 11.90661 11.90218 11.89662 11.88973
## [297] 11.88204 11.87421 11.86626 11.85819 11.85000 11.84171 11.83332 11.82483
## [305] 11.81625 11.80758 11.79884 11.79002 11.78114 11.77220 11.76320 11.75414
## [313] 11.74505 11.73592 11.72675 11.71506 11.69886 11.67893 11.65604 11.63097
## [321] 11.60449 11.57738 11.55041 11.52435 11.49998 11.47808 11.45941 11.44475
## [329] 11.43488 11.42612 11.41462 11.40085 11.38527 11.36838 11.35063 11.33250
## [337] 11.31448 11.29702 11.28061 11.26572 11.25282 11.24239 11.23490 11.23083
## [345] 11.23065 11.23333 11.23748 11.24300 11.24982 11.25786 11.26703 11.27725
## [353] 11.28844 11.30052 11.31339 11.32699 11.34123 11.35602 11.37129 11.38695
## [361] 11.40292 11.41912 11.43546 11.45186 11.47019 11.49211 11.51730 11.54541
## [369] 11.57609 11.60901 11.64381 11.68016 11.71772 11.75613 11.79505 11.83415
## [377] 11.87308 11.91150 11.94906 11.98541 12.02023 12.05316 12.08385 12.11198
## [385] 12.13719 12.16342 12.19430 12.22901 12.26673 12.30662 12.34788 12.38968
## [393] 12.43120 12.47161 12.51011 12.54585 12.57803 12.60582 12.62841 12.64914
## [401] 12.67168 12.69569 12.72081 12.74668 12.77295 12.79927 12.82529 12.85065
## [409] 12.87500 12.89799 12.91926 12.93845 12.95523 12.96922 12.98009 12.98798
## [417] 12.99346 12.99681 12.99830 12.99820 12.99679 12.99433 12.99110 12.98737
## [425] 12.98341 12.97949 12.97589 12.97288 12.97073 12.96740 12.96087 12.95147
## [433] 12.93952 12.92535 12.90929 12.89164 12.87275 12.85293 12.83251 12.81181
## [441] 12.79115 12.77086 12.75127 12.73269 12.71545 12.69988 12.68629 12.67502
## [449] 12.66298 12.64728 12.62852 12.60732 12.58427 12.55998 12.53506 12.51011
## [457] 12.48573 12.46254 12.44113 12.42212 12.40610 12.39369 12.38271 12.37070
## [465] 12.35785 12.34435 12.33036 12.31608 12.30169 12.28737 12.27329 12.25965
## [473] 12.24662 12.23438 12.22313 12.21303 12.20427 12.19704 12.19059 12.18413
## [481] 12.17773 12.17147 12.16543 12.15969 12.15433 12.14941 12.14503 12.14125
## [489] 12.13817 12.13584 12.13437 12.13381 12.13439 12.13621 12.13917 12.14318
## [497] 12.14814 12.15395 12.16051 12.16774 12.17552 12.18377 12.19239 12.20128
## [505] 12.21035 12.21949 12.22861 12.23762 12.24641 12.25489 12.26297 12.27278
## [513] 12.28620 12.30270 12.32174 12.34281 12.36538 12.38892 12.41291 12.43682
## [521] 12.46012 12.48229 12.50280 12.52113 12.53675 12.54914 12.55776 12.56462
## [529] 12.57203 12.57990 12.58814 12.59668 12.60543 12.61432 12.62325 12.63215
## [537] 12.64093 12.64952 12.65783 12.66578 12.67328 12.68026 12.68663 12.69231
## [545] 12.69722 12.70127 12.70439 12.70650 12.70750 12.70732 12.70587 12.70308
## [553] 12.69886 12.69391 12.68891 12.68380 12.67849 12.67291 12.66696 12.66058
## [561] 12.65369 12.64620 12.63804 12.62913 12.61938 12.60873 12.59708 12.58469
## [569] 12.57186 12.55857 12.54479 12.53051 12.51570 12.50036 12.48446 12.46798
## [577] 12.45091 12.43322 12.41489 12.39592 12.37627 12.35593 12.33488 12.31294
## [585] 12.28995 12.26597 12.24106 12.21527 12.18864 12.16123 12.13309 12.10428
## [593] 12.07484 12.04483 12.01430 11.98330 11.95188
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 597)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.62217 12.61782 12.61358 12.60945 12.60543 12.60150 12.59767 12.59393
## [9] 12.59029 12.58672 12.58324 12.57984 12.57652 12.57326 12.57007 12.56695
## [17] 12.56389 12.56088 12.55793 12.55502 12.55217 12.54935 12.54657 12.54383
## [25] 12.54112 12.53844 12.53578 12.53314 12.53052 12.52791 12.52532 12.52275
## [33] 12.52021 12.51769 12.51521 12.51276 12.51036 12.50800 12.50569 12.50343
## [41] 12.50123 12.49909 12.49701 12.49501 12.49307 12.49122 12.48944 12.48775
## [49] 12.48615 12.48464 12.48323 12.48193 12.48072 12.47963 12.47865 12.47778
## [57] 12.47704 12.47642 12.47594 12.47558 12.47536 12.47529 12.47536 12.47558
## [65] 12.47591 12.47631 12.47679 12.47734 12.47797 12.47869 12.47948 12.48036
## [73] 12.48133 12.48238 12.48353 12.48477 12.48610 12.48754 12.48907 12.49070
## [81] 12.49244 12.49428 12.49623 12.49829 12.50046 12.50275 12.50515 12.50767
## [89] 12.51032 12.51308 12.51597 12.51899 12.52213 12.52541 12.52882 12.53236
## [97] 12.53604 12.53986 12.54382 12.54779 12.55164 12.55537 12.55901 12.56256
## [105] 12.56605 12.56949 12.57288 12.57626 12.57961 12.58298 12.58636 12.58977
## [113] 12.59322 12.59674 12.60032 12.60400 12.60777 12.61167 12.61569 12.61985
## [121] 12.62417 12.62867 12.63335 12.63822 12.64332 12.64864 12.65462 12.66158
## [129] 12.66940 12.67796 12.68711 12.69673 12.70670 12.71688 12.72715 12.73737
## [137] 12.74742 12.75717 12.76648 12.77524 12.78330 12.79055 12.79927 12.81146
## [145] 12.82657 12.84405 12.86334 12.88388 12.90512 12.92651 12.94749 12.96751
## [153] 12.98602 13.00245 13.01625 13.02687 13.03744 13.05121 13.06776 13.08670
## [161] 13.10764 13.13017 13.15390 13.17843 13.20337 13.22831 13.25286 13.27662
## [169] 13.29919 13.32017 13.33917 13.35578 13.36962 13.38028 13.38737 13.39257
## [177] 13.39780 13.40299 13.40809 13.41302 13.41773 13.42214 13.42620 13.42984
## [185] 13.43300 13.43560 13.43760 13.43892 13.43950 13.43928 13.43819 13.43616
## [193] 13.43315 13.42907 13.42386 13.41747 13.40982 13.40086 13.38934 13.37429
## [201] 13.35608 13.33506 13.31159 13.28602 13.25872 13.23005 13.20036 13.17001
## [209] 13.13935 13.10875 13.07857 13.04916 13.02088 12.99408 12.96914 12.94640
## [217] 12.92622 12.90505 12.87951 12.85022 12.81781 12.78290 12.74612 12.70808
## [225] 12.66942 12.63074 12.59269 12.55587 12.52091 12.48844 12.45908 12.43346
## [233] 12.41218 12.39276 12.37236 12.35114 12.32927 12.30690 12.28419 12.26131
## [241] 12.23841 12.21565 12.19319 12.17120 12.14982 12.12923 12.10957 12.09101
## [249] 12.07372 12.05784 12.04354 12.03097 12.02063 12.01268 12.00680 12.00267
## [257] 11.99999 11.99843 11.99768 11.99742 11.99734 11.99711 11.99643 11.99497
## [265] 11.99242 11.98847 11.98593 11.98749 11.99256 12.00058 12.01098 12.02319
## [273] 12.03665 12.05078 12.06503 12.07881 12.09156 12.10272 12.11171 12.11796
## [281] 12.12092 12.12000 12.11823 12.11873 12.12108 12.12487 12.12971 12.13516
## [289] 12.14083 12.14631 12.15117 12.15503 12.15745 12.15804 12.15639 12.15207
## [297] 12.14550 12.13743 12.12801 12.11739 12.10572 12.09313 12.07977 12.06579
## [305] 12.05133 12.03654 12.02156 12.00654 11.99163 11.97696 11.96269 11.94895
## [313] 11.93590 11.92367 11.91242 11.89908 11.88101 11.85901 11.83388 11.80643
## [321] 11.77745 11.74775 11.71811 11.68935 11.66226 11.63764 11.61630 11.59903
## [329] 11.58662 11.57538 11.56135 11.54503 11.52688 11.50740 11.48706 11.46633
## [337] 11.44571 11.42566 11.40667 11.38921 11.37378 11.36083 11.35087 11.34436
## [345] 11.34178 11.34200 11.34353 11.34630 11.35025 11.35531 11.36142 11.36852
## [353] 11.37654 11.38542 11.39509 11.40549 11.41657 11.42824 11.44046 11.45315
## [361] 11.46625 11.47971 11.49344 11.50740 11.52342 11.54313 11.56620 11.59226
## [369] 11.62097 11.65197 11.68490 11.71943 11.75518 11.79181 11.82897 11.86630
## [377] 11.90346 11.94008 11.97582 12.01032 12.04323 12.07420 12.10288 12.12890
## [385] 12.15192 12.17530 12.20220 12.23201 12.26411 12.29789 12.33271 12.36798
## [393] 12.40306 12.43735 12.47022 12.50105 12.52923 12.55413 12.57515 12.59485
## [401] 12.61602 12.63838 12.66165 12.68554 12.70977 12.73406 12.75812 12.78167
## [409] 12.80442 12.82610 12.84642 12.86509 12.88183 12.89636 12.90839 12.91910
## [417] 12.92977 12.94029 12.95055 12.96044 12.96984 12.97864 12.98673 12.99399
## [425] 13.00032 13.00561 13.00973 13.01258 13.01404 13.01333 13.00991 13.00401
## [433] 12.99590 12.98580 12.97399 12.96069 12.94615 12.93064 12.91438 12.89763
## [441] 12.88064 12.86365 12.84691 12.83066 12.81517 12.80066 12.78739 12.77561
## [449] 12.76227 12.74461 12.72332 12.69912 12.67270 12.64474 12.61596 12.58705
## [457] 12.55871 12.53163 12.50652 12.48407 12.46497 12.44994 12.43576 12.41907
## [465] 12.40026 12.37976 12.35798 12.33534 12.31224 12.28911 12.26635 12.24438
## [473] 12.22362 12.20447 12.18735 12.17268 12.16087 12.15233 12.14476 12.13578
## [481] 12.12571 12.11488 12.10361 12.09222 12.08103 12.07037 12.06056 12.05192
## [489] 12.04478 12.03945 12.03626 12.03553 12.03693 12.03982 12.04409 12.04962
## [497] 12.05630 12.06401 12.07263 12.08204 12.09213 12.10279 12.11389 12.12532
## [505] 12.13696 12.14870 12.16041 12.17199 12.18332 12.19427 12.20474 12.21744
## [513] 12.23475 12.25601 12.28056 12.30775 12.33693 12.36743 12.39861 12.42981
## [521] 12.46038 12.48965 12.51698 12.54171 12.56318 12.58074 12.59373 12.60463
## [529] 12.61629 12.62861 12.64151 12.65489 12.66866 12.68273 12.69701 12.71141
## [537] 12.72583 12.74019 12.75439 12.76835 12.78197 12.79515 12.80782 12.81987
## [545] 12.83122 12.84178 12.85145 12.86014 12.86776 12.87422 12.87944 12.88331
## [553] 12.88574 12.88753 12.88947 12.89145 12.89338 12.89517 12.89671 12.89791
## [561] 12.89868 12.89891 12.89852 12.89740 12.89546 12.89260 12.88873 12.88412
## [569] 12.87913 12.87372 12.86789 12.86162 12.85490 12.84771 12.84003 12.83185
## [577] 12.82316 12.81394 12.80416 12.79383 12.78292 12.77141 12.75930 12.74635
## [585] 12.73239 12.71747 12.70166 12.68501 12.66759 12.64944 12.63064 12.61124
## [593] 12.59130 12.57088 12.55004 12.52884 12.50733
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 597)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.98382 11.98081 11.97789 11.97507 11.97233 11.96966 11.96706 11.96453
## [9] 11.96205 11.95963 11.95724 11.95490 11.95258 11.95028 11.94801 11.94574
## [17] 11.94348 11.94121 11.93893 11.93664 11.93432 11.93198 11.92959 11.92717
## [25] 11.92469 11.92216 11.91957 11.91690 11.91416 11.91134 11.90843 11.90542
## [33] 11.90230 11.89908 11.89575 11.89229 11.88871 11.88505 11.88131 11.87749
## [41] 11.87360 11.86967 11.86568 11.86166 11.85762 11.85355 11.84947 11.84540
## [49] 11.84133 11.83728 11.83325 11.82926 11.82532 11.82143 11.81760 11.81384
## [57] 11.81016 11.80658 11.80309 11.79971 11.79644 11.79330 11.79030 11.78744
## [65] 11.78474 11.78219 11.77982 11.77762 11.77562 11.77381 11.77221 11.77044
## [73] 11.76814 11.76534 11.76209 11.75843 11.75438 11.75001 11.74533 11.74040
## [81] 11.73525 11.72992 11.72445 11.71887 11.71324 11.70757 11.70193 11.69634
## [89] 11.69084 11.68547 11.68028 11.67530 11.67056 11.66612 11.66200 11.65825
## [97] 11.65491 11.65201 11.64959 11.64770 11.64637 11.64565 11.64556 11.64615
## [105] 11.64746 11.64953 11.65211 11.65492 11.65798 11.66128 11.66484 11.66866
## [113] 11.67274 11.67710 11.68173 11.68664 11.69184 11.69734 11.70314 11.70925
## [121] 11.71566 11.72240 11.72946 11.73685 11.74457 11.75264 11.76105 11.76982
## [129] 11.78027 11.79356 11.80939 11.82746 11.84749 11.86918 11.89224 11.91639
## [137] 11.94133 11.96676 11.99240 12.01796 12.04314 12.06766 12.09122 12.11352
## [145] 12.13429 12.15322 12.17003 12.18823 12.21103 12.23768 12.26742 12.29950
## [153] 12.33318 12.36771 12.40233 12.43629 12.46884 12.49924 12.52673 12.55056
## [161] 12.56998 12.58793 12.60770 12.62905 12.65173 12.67550 12.70012 12.72533
## [169] 12.75091 12.77660 12.80216 12.82735 12.85193 12.87564 12.89826 12.91953
## [177] 12.93921 12.95706 12.97284 12.98630 12.99719 13.00528 13.01195 13.01866
## [185] 13.02534 13.03188 13.03820 13.04421 13.04981 13.05492 13.05943 13.06327
## [193] 13.06633 13.06853 13.06977 13.06997 13.06903 13.06686 13.06337 13.05846
## [201] 13.05205 13.04405 13.03436 13.02288 13.00760 12.98703 12.96188 12.93290
## [209] 12.90081 12.86633 12.83020 12.79315 12.75590 12.71918 12.68372 12.65025
## [217] 12.61950 12.59219 12.56905 12.54482 12.51442 12.47888 12.43921 12.39642
## [225] 12.35154 12.30557 12.25954 12.21447 12.17137 12.13125 12.09514 12.06405
## [233] 12.03900 12.01682 11.99374 11.96991 11.94549 11.92062 11.89546 11.87013
## [241] 11.84481 11.81963 11.79474 11.77030 11.74644 11.72333 11.70109 11.67990
## [249] 11.65988 11.64120 11.62399 11.60842 11.59478 11.58312 11.57316 11.56465
## [257] 11.55731 11.55088 11.54509 11.53967 11.53436 11.52888 11.52298 11.51638
## [265] 11.50882 11.50002 11.49268 11.48928 11.48929 11.49217 11.49740 11.50443
## [273] 11.51273 11.52177 11.53100 11.53990 11.54793 11.55456 11.55925 11.56146
## [281] 11.56066 11.55632 11.55098 11.54734 11.54512 11.54403 11.54378 11.54408
## [289] 11.54464 11.54517 11.54538 11.54499 11.54369 11.54121 11.53725 11.53153
## [297] 11.52370 11.51378 11.50201 11.48861 11.47383 11.45790 11.44106 11.42354
## [305] 11.40557 11.38740 11.36925 11.35137 11.33399 11.31734 11.30166 11.28719
## [313] 11.27416 11.26280 11.25336 11.24231 11.22652 11.20683 11.18406 11.15904
## [321] 11.13260 11.10557 11.07877 11.05305 11.02922 11.00811 10.99055 10.97738
## [329] 10.96941 10.96416 10.95864 10.95300 10.94735 10.94184 10.93660 10.93176
## [337] 10.92745 10.92380 10.92094 10.91901 10.91814 10.91846 10.92010 10.92320
## [345] 10.92788 10.93464 10.94376 10.95503 10.96824 10.98319 10.99968 11.01750
## [353] 11.03645 11.05632 11.07690 11.09799 11.11940 11.14090 11.16230 11.18340
## [361] 11.20398 11.22385 11.24280 11.26062 11.27918 11.30030 11.32374 11.34923
## [369] 11.37654 11.40540 11.43557 11.46679 11.49880 11.53136 11.56422 11.59712
## [377] 11.62980 11.66202 11.69353 11.72407 11.75338 11.78122 11.80734 11.83147
## [385] 11.85338 11.87534 11.89953 11.92554 11.95295 11.98133 12.01028 12.03938
## [393] 12.06820 12.09634 12.12336 12.14887 12.17243 12.19364 12.21207 12.22972
## [401] 12.24871 12.26878 12.28966 12.31110 12.33284 12.35462 12.37619 12.39728
## [409] 12.41764 12.43701 12.45513 12.47174 12.48659 12.49941 12.50995 12.51886
## [417] 12.52700 12.53438 12.54106 12.54706 12.55241 12.55716 12.56134 12.56498
## [425] 12.56811 12.57077 12.57300 12.57483 12.57629 12.57639 12.57424 12.57005
## [433] 12.56405 12.55645 12.54745 12.53727 12.52613 12.51423 12.50180 12.48904
## [441] 12.47617 12.46340 12.45094 12.43902 12.42783 12.41760 12.40854 12.40087
## [449] 12.39255 12.38169 12.36869 12.35394 12.33785 12.32081 12.30321 12.28546
## [457] 12.26796 12.25110 12.23527 12.22088 12.20833 12.19800 12.18820 12.17707
## [465] 12.16482 12.15164 12.13775 12.12333 12.10859 12.09374 12.07897 12.06448
## [473] 12.05047 12.03715 12.02472 12.01337 12.00331 11.99474 11.98615 11.97612
## [481] 11.96493 11.95289 11.94030 11.92746 11.91467 11.90224 11.89045 11.87962
## [489] 11.87004 11.86201 11.85583 11.85181 11.84898 11.84620 11.84354 11.84103
## [497] 11.83874 11.83670 11.83498 11.83363 11.83269 11.83221 11.83225 11.83286
## [505] 11.83409 11.83599 11.83861 11.84200 11.84621 11.85130 11.85731 11.86555
## [513] 11.87700 11.89122 11.90778 11.92624 11.94618 11.96715 11.98873 12.01048
## [521] 12.03197 12.05275 12.07241 12.09050 12.10659 12.12025 12.13105 12.14072
## [529] 12.15124 12.16253 12.17450 12.18706 12.20013 12.21363 12.22746 12.24154
## [537] 12.25579 12.27011 12.28443 12.29866 12.31271 12.32649 12.33992 12.35291
## [545] 12.36538 12.37725 12.38841 12.39880 12.40832 12.41689 12.42442 12.43082
## [553] 12.43601 12.44071 12.44561 12.45065 12.45576 12.46084 12.46583 12.47064
## [561] 12.47520 12.47944 12.48327 12.48661 12.48940 12.49155 12.49298 12.49395
## [569] 12.49474 12.49533 12.49570 12.49584 12.49572 12.49533 12.49465 12.49365
## [577] 12.49232 12.49063 12.48858 12.48614 12.48329 12.48001 12.47628 12.47191
## [585] 12.46675 12.46086 12.45428 12.44706 12.43927 12.43096 12.42218 12.41297
## [593] 12.40341 12.39353 12.38340 12.37306 12.36257
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")